﻿Imports DevExpress.XtraEditors
Imports System.IO
Imports System.Linq
Imports System.Data.Linq
Imports SERM.IModule
Imports SERM.Core


Public Class MainReady

    Structure clientRect
        Public location As Point
        Public width As Integer
        Public height As Integer
    End Structure
    Dim _restore As clientRect
    Dim _Fullscreen As Boolean = False
    Dim _tmpButton As List(Of GroupMuduleButton)
    Dim _level1Width As Integer = 0
    Dim _btnAllWidth As Integer = 0

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        'Try
        '    Me.Icon = SERM.Erp.Main.My.Resources.serm_ico_256px

        '    Dim logon As LogOnForm

        '    logon = New LogOnForm()

        '    If logon.ShowDialog() <> Windows.Forms.DialogResult.OK Then
        '        Me.Close()
        '        Return

        '    End If

        'Catch ex As Exception

        'End Try

    End Sub

    Private Sub CreateReady()

        _tmpButton = New List(Of GroupMuduleButton)
        CreateLevel1InPanelControl("ACC", "ระบบบัญชี") '(Accounting System)")
        CreateLevel1InPanelControl("ACR", "ระบบลูกหนี้") ' (Accounts Receivable)")
        CreateLevel1InPanelControl("ACP", "ระบบเจ้าหนี้") ' (Accounts Payable)")
        CreateLevel1InPanelControl("GL", "ระบบบัญชีแยกประเภท") ' (General Ledger)")
        CreateLevel1InPanelControl("SAL", "ระบบงานขาย") ' (Sales System)")
        CreateLevel1InPanelControl("PUR", "ระบบจัดซื้อ") ' (Purchasing System) ")
        CreateLevel1InPanelControl("INV", "ระบบสินค้าคงคลัง") ' (Inventory System)")
        CreateLevel1InPanelControl("ASS", "ระบบสินทรัพย์") '(Asset System)")
        CreateLevel1InPanelControl("PERS", "ระบบบุคคล") ' (Personal System)")
        CreateLevel1InPanelControl("INFO", "ระบบรายงาน") ' (Information Systems)")
    End Sub

    Private Function CreateTileItem(ByVal Name As String, ByVal caption As String, ByVal imgUrl As String, ByVal refModul As String) As TileItem
        Dim newTile As TileItem
        Dim img As Image
        Dim ele As String()
        Dim _el1 As TileItemElement
        Dim _el2 As TileItemElement

        ele = caption.Replace("<br>", "$").Replace("<br/>", "$").Split("$")
        If (File.Exists(imgUrl)) Then
            img = Image.FromFile(imgUrl)
        End If

        newTile = New TileItem()
        newTile.Name = Name
        newTile.Tag = refModul
        newTile.Elements.Add(New TileItemElement())

        _el1 = New TileItemElement()
        _el2 = New TileItemElement()
        _el1.Text = ele(0)
        If ele.Length >= 2 Then

            _el2.Text = ele(1)
        End If
        _el1.Appearance.Normal.ForeColor = Color.White
        _el1.Appearance.Hovered.ForeColor = Color.White
        _el1.Appearance.Selected.ForeColor = Color.White
        newTile.Elements.Add(_el1)

        If (Screen.GetWorkingArea(Me).Width > 1024) Then
            _el2.Appearance.Normal.Font = New Font(_el2.Appearance.Normal.Font.FontFamily, _el2.Appearance.Normal.Font.Size + 2.0F)
            _el2.Appearance.Hovered.Font = _el2.Appearance.Normal.Font
            _el2.Appearance.Selected.Font = _el2.Appearance.Normal.Font
        End If

        newTile.Elements.Add(_el2)
        newTile.IsLarge = True
        newTile.AllowHtmlText = DevExpress.Utils.DefaultBoolean.True
        'newTile.Image = img
        newTile.ImageAlignment = TileItemContentAlignment.TopLeft

        _el2.Appearance.Normal.ForeColor = Color.White
        _el2.Appearance.Hovered.ForeColor = Color.White
        _el2.Appearance.Selected.ForeColor = Color.White
        Return newTile
    End Function

    Private Function CreateLevel1InPanelControl(ByVal Name As String, ByVal Caption As String)
        Dim Button As GroupMuduleButton
        Button = New GroupMuduleButton()
        Button.Name = Name
        Button.Caption = Caption
        Button.Size = New Size(120, 50)
        Button.Location = New Point(_btnAllWidth, 0)
        AddHandler Button.OnSelectItem, AddressOf GroupMuduleButton1_OnSelectItem
        _tmpButton.Add(Button)
        _btnAllWidth += Button.Size.Width
        pnlMnuLevel1.Controls.Add(Button)
    End Function

    Private Function CreateLevel1InScollControl(ByVal Name As String, ByVal Caption As String)
        Dim Button As GroupMuduleButton
        Button = New GroupMuduleButton()
        Button.Name = Name
        Button.Caption = Caption
        Button.Size = New Size(120, 50)
        Button.Location = New Point(_btnAllWidth, 0)
        AddHandler Button.OnSelectItem, AddressOf GroupMuduleButton1_OnSelectItem
        _tmpButton.Add(Button)
        _btnAllWidth += Button.Size.Width
        XtraScrollableControl1.Controls.Add(Button)
    End Function

    Private Sub GroupMuduleButton1_OnSelectItem(sender As System.Object, e As System.EventArgs)
        Dim Button As GroupMuduleButton
        Button = CType(sender, GroupMuduleButton)
        For Each btn As GroupMuduleButton In _tmpButton
            btn.Selected = False
        Next
        Button.Selected = True
        TileControl1.Groups.Clear()
        Dim tg As TileGroup = New TileGroup()
        tg.Name = Button.Name
        If Button.Name.Equals("GL") Then
            tg.Items.Add(CreateTileItem("GL1", "GL Accounting Document", "", ""))
            tg.Items.Add(CreateTileItem("GL2", "General Document Posting", "", ""))
            tg.Items.Add(CreateTileItem("GL3", "Account Assignment", "", ""))
            tg.Items.Add(CreateTileItem("GL4", "Account Display Balance", "", ""))
            tg.Items.Add(CreateTileItem("GL5", "Account Clearing", "", ""))
        ElseIf Button.Name.Equals("ACC") Then
            tg.Items.Add(CreateTileItem("ACC1", "Receipt Document", "", ""))
            tg.Items.Add(CreateTileItem("ACC2", "Payment Document", "", ""))
            tg.Items.Add(CreateTileItem("ACC3", "Accounting Document", "", ""))
            tg.Items.Add(CreateTileItem("ACC4", "G/L Account Document", "", ""))
            tg.Items.Add(CreateTileItem("ACC5", "Tax Document ", "", ""))
        ElseIf Button.Name.Equals("ACR") Then
            tg.Items.Add(CreateTileItem("ACR1", "Customer Master", "", ""))
            tg.Items.Add(CreateTileItem("ACR2", "AR General Invoice", "", ""))
            tg.Items.Add(CreateTileItem("ACR3", "Credit Memo", "", ""))
            tg.Items.Add(CreateTileItem("ACR4", "Debit Memo", "", ""))
            tg.Items.Add(CreateTileItem("ACR5", "Receive Clearing Document", "", ""))
            tg.Items.Add(CreateTileItem("ACR6", "Withholding Tax", "", ""))
            tg.Items.Add(CreateTileItem("ACR7", "VAT", "", ""))
        ElseIf Button.Name.Equals("ACP") Then
            tg.Items.Add(CreateTileItem("ACP1", "Vendor Master", "", ""))
            tg.Items.Add(CreateTileItem("ACP2", "AP General Invoice", "", ""))
            tg.Items.Add(CreateTileItem("ACP3", "Credit Memo", "", ""))
            tg.Items.Add(CreateTileItem("ACP4", "Debit Memo", "", ""))
            tg.Items.Add(CreateTileItem("ACP5", "Down Payment Document", "", ""))
            tg.Items.Add(CreateTileItem("ACP6", "Payment Request Documen", "", ""))
            tg.Items.Add(CreateTileItem("ACP7", "Payment Clearing Document", "", ""))
        ElseIf Button.Name.Equals("SAL") Then
            tg.Items.Add(CreateTileItem("SAL1", "Quotation Document", "", ""))
            tg.Items.Add(CreateTileItem("SAL2", "Approve Quotation", "", ""))
            tg.Items.Add(CreateTileItem("SAL3", "Close Quotation", "", ""))
            tg.Items.Add(CreateTileItem("SAL4", "Sale Order", "", ""))
            tg.Items.Add(CreateTileItem("SAL5", "Sale Plan", "", ""))
        ElseIf Button.Name.Equals("PUR") Then
            tg.Items.Add(CreateTileItem("PUR1", "Purchasing Requisition", "", "SERM.Erp.PR.dll"))
            tg.Items.Add(CreateTileItem("PUR2", "Approve Purchasing Requisition", "", ""))
            tg.Items.Add(CreateTileItem("PUR3", "Close Purchasing Requisition", "", ""))
            TileControl1.Groups.Add(tg)
            tg = New TileGroup()
            tg.Items.Add(CreateTileItem("PUR4", "Purchasing Order", "", "SERM.Erp.PO.dll"))
            tg.Items.Add(CreateTileItem("PUR5", "Approve Purchasing Order", "", ""))
            tg.Items.Add(CreateTileItem("PUR6", "Close Purchasing Order", "", ""))
        ElseIf Button.Name.Equals("INV") Then
            tg.Items.Add(CreateTileItem("INV1", "Goods Movement Slip", "", ""))
            tg.Items.Add(CreateTileItem("INV2", "Approve Goods Movement Slip", "", ""))
            tg.Items.Add(CreateTileItem("INV3", "Close Goods Movement Slip", "", ""))
            tg.Items.Add(CreateTileItem("INV4", "Goods Receive", "", ""))
            tg.Items.Add(CreateTileItem("INV5", "Goods Issue", "", ""))
            tg.Items.Add(CreateTileItem("INV6", "Packing", "", ""))
            tg.Items.Add(CreateTileItem("INV7", "Goods Receive (Revert)", "", ""))
            tg.Items.Add(CreateTileItem("INV8", "Goods Issue (Revert)", "", ""))
            tg.Items.Add(CreateTileItem("INV9", "Goods Transfer", "", ""))
            tg.Items.Add(CreateTileItem("INV10", "Goods Return", "", ""))
        ElseIf Button.Name.Equals("ASS") Then
            tg.Items.Add(CreateTileItem("ASS1", "Machine Master", "", ""))
            tg.Items.Add(CreateTileItem("ASS2", "Machine Assignment", "", ""))
        ElseIf Button.Name.Equals("PERS") Then
            tg.Items.Add(CreateTileItem("PERS1", "Personal Performance Appraisal", "", ""))
            tg.Items.Add(CreateTileItem("PERS2", "Petty Cash Payment", "", ""))
            tg.Items.Add(CreateTileItem("PERS3", "Personal Accident Record", "", ""))
            tg.Items.Add(CreateTileItem("PERS4", "Personal Voucher Receive", "", ""))
        ElseIf Button.Name.Equals("INFO") Then
            tg.Items.Add(CreateTileItem("INFO1", "Report 1", "", ""))
            tg.Items.Add(CreateTileItem("INFO2", "Report 2", "", ""))
        End If
        TileControl1.Groups.Add(tg)
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub MainReady_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown

        CreateReady()
        _tmpButton.FirstOrDefault().btn.PerformClick()

    End Sub

    Private Function LoadModuls(ByVal modulename As String)
        Dim pluginPath As String
        Dim sermmodule As ISERMModule
        pluginPath = Path.Combine(Application.StartupPath, "Modules")
        sermmodule = SERM.PluginLoader.LoadPlugIn(Path.Combine(pluginPath, modulename))
        If sermmodule IsNot Nothing Then

            Dim frm As SERMTempForm = sermmodule.IntitalFrom(Nothing, SermLang.EN)
            frm.TopMost = True
            frm.Show()
            frm.TopMost = False
        End If

    End Function

    Private Function Fullscreen(ByVal isfullscreen As Boolean)
        Me.WindowState = FormWindowState.Maximized
        ' Me.MinimumSize = Me.MaximumSize
        'Me.MaximizeBox = False
        'Me.MinimizeBox = False
    End Function


    Private Sub TileControl1_ItemClick(sender As System.Object, e As DevExpress.XtraEditors.TileItemEventArgs) Handles TileControl1.ItemClick
        LoadModuls(e.Item.Tag)
    End Sub

    Private Sub MainReady_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Fullscreen(True)
        'Me.MaximumSize = New Size(My.Computer.Screen.WorkingArea.Size.Width, _
        '                       My.Computer.Screen.WorkingArea.Size.Height - 100)
        'Me.Width = Screen.PrimaryScreen.WorkingArea.Width
        'Me.Height = Screen.PrimaryScreen.WorkingArea.Height

    End Sub
End Class
